package com.mendmix.tutorial.service.impl;

import com.mendmix.tutorial.dao.ActorDao;
import com.mendmix.tutorial.domain.Actor;
import com.mendmix.tutorial.service.IActorService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

@Component
public class ActorServiceImpl implements IActorService {

    @Autowired
    private ActorDao actorDao;

    @Override
    public List<Actor> listActors() {
        Iterable<Actor> all = actorDao.findAll();
        List<Actor> list = new ArrayList<>();
        all.forEach(list::add);
        return list;
    }

    @Override
    public Actor getActor(Integer actorId) {
        return actorDao.getActorByActorId(actorId);
    }

    @Override
    public boolean deleteActor(Integer actorId) {
        actorDao.deleteById(actorId);
        return true;
    }

    @Override
    public Actor addActor(String firstName, String lastName) {
        Actor actor = new Actor();
        actor.setFirstName(firstName);
        actor.setLastName(lastName);
        actor.setLastUpdate(LocalDateTime.now());
        actorDao.save(actor);
        System.out.println(actor);
        return actor;
    }
}
